import 'dart:async';
import 'dart:ffi';

import 'package:flutter/material.dart';

///通用的轮播组件
///自动轮播、轮播小点
///[width] 宽度
///[height] 高度
/// [decoration] 轮播组件的容器样式
/// [getUrl] 返回每一个item的url
/// [marginBottom] 小点距离底部的高度
/// [size] 数据源的长度
///
class SwiperWidget<T> extends StatefulWidget {
  final double width;
  final double height;
  final BoxDecoration decoration;
  final String Function(int) getUrl;
  final double marginBottom;
  final int size;

  SwiperWidget(
      {Key key,
      this.width,
      this.height,
      this.decoration,
      this.getUrl,
      this.marginBottom = 0, this.size}) {
    assert(this.width != null || this.height != null);
  }

  @override
  State<StatefulWidget> createState() => _SwiperWidget();
}

class _SwiperWidget extends State<SwiperWidget> {
  ValueNotifier _notifier=ValueNotifier<int>(0);
  PageController _pageController=PageController();
  Timer _timer;
  int index=0;

  @override
  void initState() {
    super.initState();
    initTimer();
  }

  @override
  void dispose() {
    super.dispose();
   if(_timer!=null){
     if(_timer.isActive){
       _timer.cancel();
     }
     _timer=null;
   }
   _pageController.dispose();
  }

  void initTimer(){
    if(_timer==null){
      _timer=Timer.periodic(Duration(seconds: 3), (timer) {
        index++;
        _pageController.animateToPage(index,duration: Duration(milliseconds: 300),curve: Curves.linear);
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: widget.decoration,
      width: widget.width,
      height: widget.height,
      alignment: Alignment.center,
      child: Stack(
        children: [
          PageView.builder(
            physics:AlwaysScrollableScrollPhysics() ,
            onPageChanged: (i){
              _notifier.value=i%widget.size;
            },
            controller: _pageController,
              itemBuilder: (_, index) {
              this.index=index;
            return Image.network(
              widget.getUrl(index%widget.size),
              width: widget.width,
              height: widget.height,
            );
          }),
          _buildPoint()
        ],
      ),
    );
  }

  Widget _buildPoint() {
    return Align(
        alignment: Alignment.bottomCenter,
        child: Container(
          height: 2,
          margin: EdgeInsets.only(bottom: widget.marginBottom),
          child: ValueListenableBuilder(
            valueListenable:_notifier ,
            builder: (context,data,child){
              return  ListView.builder(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                scrollDirection: Axis.horizontal,
                  itemBuilder:(_,index){
                    return Container(
                      margin: EdgeInsets.only(left: 2,right: 2),
                      decoration: BoxDecoration(
                          color:data==index?Colors.red:Colors.white,
                          borderRadius: BorderRadius.all(Radius.circular(1))),
                      width: 2,
                      height: 2,
                    );

                  },
              itemCount: widget.size,);
            },
          ) ,
        ));
  }
}
